package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/23.
 */
public class ModelAdapter extends BaseExpandableListAdapter {
    Context context;

    JSONArray jsa;

    int groupselectPosition = -1;
    int childselectPosition = -1;

    public ModelAdapter(Context context) {
        this.context = context;
    }


    public void setData(JSONArray jsa) {
        this.jsa = jsa;
        notifyDataSetChanged();
    }

    @Override
    public JSONObject getChild(int groupPosition, int childPosition) {
        try {
            JSONObject jo = jsa.getJSONObject(groupPosition);
            JSONArray modeljsa = jo.getJSONArray("models");
            return modeljsa.getJSONObject(childPosition);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        try {
            JSONObject jo = jsa.getJSONObject(groupPosition);
            JSONArray modeljsa = jo.getJSONArray("models");
            if (modeljsa == null) {
                return 0;
            }
            return modeljsa.length();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public void setCurrentSelectPosition(int goupposition, int childposition) {
        this.groupselectPosition = goupposition;
        this.childselectPosition = childposition;
//        if (groupselectPosition != -1) {
//            com.gongpingjia.constant.Const.ModelgroupPositon = groupselectPosition;
//        }
//
//        if (childselectPosition != -1) {
//            com.gongpingjia.constant.Const.ModelchildPositon = childselectPosition;
//        }
        notifyDataSetChanged();
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_model_item, null);
        }


        ImageView picI = (ImageView) convertView.findViewById(R.id.modelLogo);
        TextView nameT = (TextView) convertView.findViewById(R.id.modelText);
        ImageView currentI = (ImageView) convertView.findViewById(R.id.current);
        View BgV = convertView.findViewById(R.id.bg);


        JSONObject jo = getChild(groupPosition, childPosition);


        try {
            nameT.setText(jo.getString("name"));
            String filename = jo.getString("logo_img");
            if (null != filename && !filename.isEmpty() && !filename.equals("")) {
                String model_logo_url =
                        Const.IMG_DOMAIN
                                + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                                + filename + "?imageView2/0/w/200/h/100";

                ImageLoad.LoadImage(picI, model_logo_url, R.drawable.brandnull, R.drawable.brandnull,context);
                //  Glide.with(ModelFragment.this).load(model_logo_url).placeholder(R.drawable.brandnull).into(modelHolder.modelImage);
            } else {
                picI.setImageResource(R.drawable.carnull);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (groupselectPosition == groupPosition && childselectPosition == childPosition) {
            currentI.setVisibility(View.VISIBLE);
            BgV.setBackgroundColor(context.getResources().getColor(R.color.select_list_bg));
        } else {
            currentI.setVisibility(View.INVISIBLE);
            BgV.setBackgroundColor(context.getResources().getColor(R.color.nothing));
        }

        return convertView;
    }

    @Override
    public JSONObject getGroup(int groupPosition) {
        try {
            JSONObject jo = jsa.getJSONObject(groupPosition);
            return jo;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int getGroupCount() {
        if (jsa == null) {
            return 0;
        }
        return jsa.length();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_model_group_list, null);
        }

        TextView nameT = (TextView) convertView.findViewById(R.id.name);
        JSONObject jo = getGroup(groupPosition);
        try {
            nameT.setText(jo.getString("mum"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //textView.setTextSize(16);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

